﻿function PrepareLightbox(callback){
		
		if($("#lightbox").length == 0){
			$("body").append("<div class='overlay'></div>");
			$(".overlay").css({
				"opacity": 0, 
				"position":"fixed",
				"top":0, 
				"left":0, 
				"width": "100%", 
				"height": "100%", 
				"background":"#000", 
				"z-index":5});
			
			$(".overlay").animate({"opacity":0.7},200, function(){
			
				$("body").append('<div id="lightbox"></div>');
				$("#lightbox").css({
					"z-index":6,
					"position":"fixed",
					"background":"#FFF",
					"top":"50%",
					"left":"50%",
					"padding":"11px",
					"width":"590px",
					"height":"350px",
					"margin-left":"-295px",
					"margin-top":"-175px",
					"opacity":0
				});
				$("#lightbox").animate({"opacity":1}, 200, function(){
					$("#lightbox").append("<a href='#' id='close_lightbox'><img src='/Control/ArquivoExibir.aspx?NomeArquivo=btn_close.gif' alt='' /></a>");
					callback();
				});
				
			});
		}else{
			$("#lightbox").empty();
			$("#lightbox").append("<a href='#' id='close_lightbox'><img src='/Control/ArquivoExibir.aspx?NomeArquivo=btn_close.gif' alt='' /></a>");
			callback();
		}
}

jQuery.fn.smartLabels = function(){

	$(this).find("input:text, textarea").each(function(){
		if($(this).attr('value') !== undefined){
			$(this).data("originalText", $(this).attr('value'));
		}else{
			$(this).data("originalText", $(this).text());
		}
		
		$(this).bind("focus", function(){
			if($(this).attr("value") === $(this).data("originalText") || $(this).val() === $(this).data("originalText")){
				if($(this).attr("value").length > 0){
					$(this).attr("value", "");
				}else{
					$(this).text("");
				}
			}
		});
		
		$(this).bind("blur", function(){
			if($(this).attr('value') !== undefined){
	
				if($(this).attr("value").length == 0){
					$(this).attr("value", $(this).data("originalText"));
				}
			}else{
				if($(this).val().length == 0){
					$(this).text($(this).data("originalText"));
				}
			}
		});
	});
}

$(function() {

	$(".right-box form, .inside-label-form").smartLabels();

	$("#page #page-content form select").jQSelect();
	$("#page #page-content form select").css("display", "none");

	$('#heading .page-name').html( $('#page-content h1 img').attr("alt") );
	
	$('#page #navigation ul li a:contains(' + $('#page-content h1 img').attr("alt") + ')').addClass('active')
	
	
	Cufon.replace('#page #page-content h1', {
        fontFamily: 'Knockout'
    });
	
	Cufon.replace(".right-box h2, #page #page-content h2, #page #page-content .right-box p .phone, p a.english", {
        fontFamily: 'Proxima Nova Rg'
    });
	
	$(".address-box .links a.map, #lightbox a.map").live('click', function(){
		if($(this).parents('.address-box').length == 1){
			$('.address-box.selected').removeClass("selected");
			$(this).parents('.address-box').addClass('selected');
			var maps_url = $(this).attr("href");
			var info_html = $(this).parents('.address-box').find(".info").html();
		}else{
			var maps_url = $(".address-box.selected .links a.map").attr("href");
			var info_html = $('.address-box.selected').find(".info").html();
		}
		PrepareLightbox(function(){
			$("#lightbox").append('<iframe width="350" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="' + maps_url + '&output=embed"></iframe>');
			$("#lightbox").append("<div class='info'></div>");
			$("#lightbox .info").append(info_html);
			$("#lightbox").append("<a href='#' class='gallery'>Ver Fotos</a>");
		});
		
		return false;
	
	});
	
	$(".address-box .links a.gallery, #lightbox a.gallery").live('click', function(){
		if($(this).parents('.address-box').length == 1){
			$('.address-box.selected').removeClass("selected");
			$(this).parents('.address-box').addClass('selected');
			var gallery_html = $(this).parents('.address-box').find("ul.gallery").html();
		}else{
			var gallery_html = $('.address-box.selected').find("ul.gallery").html();
		}
		PrepareLightbox(function(){
			//$("#lightbox").append("<div id='stage'><a href='#' id='previous'></a>" + gallery_html + "<a href='#' id='next'></a></div>");
			$("#lightbox").append(gallery_html);
			$("#lightbox #stage").find("li").each(function(){
				$(this).replaceWith(function() {
					return "<div class='entry'>" + $(this).html() + "</div>";
				});
			});
			$("#lightbox #stage .entry").css("display", "none");
			$("#lightbox #stage .entry:eq(0)").css("display", "block");
			$("#lightbox").append("<a href='#' class='map'>Como Chegar</a>");
		});
	
		return false;
	});
	
	$("#lightbox #close_lightbox").live("click", function(){
	
		$("#lightbox").animate({"opacity":0}, 200, function(){
			$(this).empty();
			$(this).remove();
			$(".overlay").animate({"opacity":0}, 200, function(){
				$(this).remove();
			});
		});
		
		return false;
	});
	
	$("#lightbox #stage #next").live("click", function(){
		var visible = $("#stage .entry:visible").index();
		var total = $("#stage .entry").length;
		if(visible == total){
			var nextEntry = 0;
		}else{
			var nextEntry = visible++;
		}
		
		$("#stage .entry:visible").css("display", "none");
		$("#stage .entry:eq(" + nextEntry + ")").css("display", "block");
		
		return false;
	});
	$("#lightbox #stage #previous").live("click", function(){
		var visible = $("#stage .entry:visible").index();
		var total = $("#stage .entry").length;
		if(visible == 1){
			var nextEntry = total - 1;
		}else{
			var nextEntry = (visible - 2);
		}
		
		$("#stage .entry:visible").css("display", "none");
		$("#stage .entry:eq(" + nextEntry + ")").css("display", "block");
		
		return false;
	});
    
    $('ol.white-list a').click(function(e) {
        

        var gotoreference = $(this).attr('href');
        gotoreference = gotoreference.replace("#","");
        
        $('html, body').animate({
                scrollTop: $( $('a[name=' + gotoreference + ']') ).offset().top
        }, 800);
    
        e.preventDefault();
    
    });
    
    
    if ( $('body').is('.newsletter') ){
        $('.jqselectlabel').html("Selecione seu gênero");
    }
    
});